package com.wframe.project.former.dataservices.marketing;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.frame.database.JBaseService;
import com.frame.database.JServiceFactory;
import com.frame.database.JdataBean;
import com.frame.database.PageResult;
import com.wframe.project.jnd.entity.Promotion;
import org.apache.commons.lang.StringUtils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/6/14.
 */
public class FullGiftsPromotionsService extends JBaseService<FullGiftsPromotionsDao> {
    public static FullGiftsPromotionsService getIntance() {
        return JServiceFactory.createJServiceProxy(FullGiftsPromotionsService.class);
    }
    private PromotionsService promotionsService = PromotionsService.getIntance();

    public void FullGiftsUpDataStop(int id, int state) throws Exception {
        getDao().FullGiftsUpDataStop(id, state);
    }

    public List<JdataBean> canCreate() throws Exception {
        return getDao().canCreate();
    }

    public List<JdataBean> canCreate(int proClass) throws Exception {
       /* List<JdataBean> threeClass = getDao().canCreate(proClass);
        List<JdataBean> twoClass = getDao().canCreateTwoClassId(proClass);
        if(threeClass.size() != 0 ) {
            return threeClass;
        }
        return twoClass;*/
       return getDao().canCreateByClassId(proClass);
    }

    public List<JdataBean> canCreate(List<String> ids) throws Exception {
        return getDao().canCreate(ids);
    }

    public JdataBean findOne(String id) throws Exception {
        return getDao().findOne(id);
    }

    public void updatFullGifts(JdataBean data, Map key) throws Exception {
        Object promotionId = key.get("id");
        getDao().delFullGifts(promotionId);
        if (StringUtils.isNotBlank(data.getString("product_type"))) {
            if (data.getInt("product_type") == Promotion.PruductType.SOME.ordinal()) {
                String products = data.getString("product_val");
                JSONArray arr = JSONArray.parseArray(products);
                getDao().saveProducts(promotionId, arr);
                data.put("product_val", promotionId);
            }
        }

        JSONArray items = JSON.parseArray(data.getString("items"));
        for (int i = 0; i < items.size(); i++) {
            Map dataMap = new HashMap();
            JSONObject item = items.getJSONObject(i);
            dataMap.put("min_money", item.getBigDecimal("min_money"));
            dataMap.put("min_number", item.getInteger("min_number"));
            dataMap.put("promotionid", promotionId);
            String id = getDao().getSession().saveReturnKey("wb_promotion_purchase_gift", dataMap);

            JSONArray gifts = item.getJSONArray("gifts");
            for (int j = 0; j < gifts.size(); j++) {
                JSONObject gift = gifts.getJSONObject(j);
                if(StringUtils.isNotBlank(gift.getString("unit")) && StringUtils.isNotBlank(gift.getString("productid"))) {
                    gift.put("giftid", id);
                    getDao().getSession().save("wb_promotion_purchase_gift_product", gift);
                }
            }
        }
        data.remove("items");
        getDao().update(data.getDataMap(), key);
    }

    public PageResult queryMoneyProduct(JdataBean bean, int pageSize, int pageNumber) throws Exception {
        return getDao().queryMoneyProduct(bean, pageSize, pageNumber);
    }

    public String saveFullGifts(JdataBean data) throws Exception {
        JSONArray items = JSON.parseArray(data.getString("items"));
        data.remove("items");
        String promotionId = null;
        if (StringUtils.isNotBlank(data.getString("product_type")) && data.getInt("product_type") == Promotion.PruductType.SOME.ordinal()) {
            JSONArray products = JSONArray.parseArray(data.getString("product_val"));
            data.remove("product_val");
            promotionId = getDao().getSession().saveReturnKey("wb_promotions", data.getDataMap());
            Map<String,String> key = new HashMap<>();
            key.put("id",promotionId);
            Map<String,String> pdata = new HashMap<>();
            pdata.put("product_val",promotionId);
            promotionsService.update(pdata,key);
            getDao().saveProducts(promotionId, products);
        } else {
            promotionId = getDao().getSession().saveReturnKey("wb_promotions", data.getDataMap());
        }

        for (int i = 0; i < items.size(); i++) {
            Map dataMap = new HashMap();
            JSONObject item = items.getJSONObject(i);
            dataMap.put("min_money", item.getBigDecimal("min_money"));
            dataMap.put("min_number", item.getInteger("min_number"));
            dataMap.put("promotionid", promotionId);
            String id = getDao().getSession().saveReturnKey("wb_promotion_purchase_gift", dataMap);

            JSONArray gifts = item.getJSONArray("gifts");
            for (int j = 0; j < gifts.size(); j++) {
                JSONObject gift = gifts.getJSONObject(j);
                if(StringUtils.isNotBlank(gift.getString("unit")) && StringUtils.isNotBlank(gift.getString("productid"))) {
                    gift.put("giftid", id);
                    getDao().getSession().save("wb_promotion_purchase_gift_product", gift);
                }
            }
        }
        return promotionId;
    }

    public void addControlsalesSingLe(String promotionid, JSONArray sales, String userids) throws Exception {
        getDao().deleteControlsalesAll(promotionid);
        String[] uids = null;
        if (StringUtils.isNotBlank(userids)) uids = userids.split(",");
        int iscontrol = 0;
        if (uids != null && uids.length > 0) iscontrol = 1;

        if (sales != null && sales.size() > 0) {
            iscontrol = 1;
        }
        getDao().updatPromotecontrol(promotionid, iscontrol);//标记控销状态
        if (sales != null) {
            for (int i = 0; i < sales.size(); i++) {
                JdataBean bean = new JdataBean();
                bean.put("promotion_id", promotionid);
                bean.put("type", sales.getJSONObject(i).getString("type"));
                bean.put("val", sales.getJSONObject(i).getString("val"));
                bean.put("text", sales.getJSONObject(i).getString("text"));
                getDao().addControlsalesSingLe(bean);

            }
        }
        if (uids != null) {
            for (String uid : uids) {
                JdataBean bean = new JdataBean();
                bean.put("promotionid", promotionid);
                bean.put("type", 4);
                bean.put("val", uid);
                getDao().addControlsalesSingLe(bean);
            }

        }
    }

    public List<JdataBean> chooseGifts(String giftName,int rows) throws Exception {
        return getDao().chooseGifts(giftName,rows);
    }
}
